#include <stdio.h>

#define MAX_TEAMS 1000

int findRoot(int parent[], int x) {
    if (parent[x] != x) {
        parent[x] = findRoot(parent, parent[x]);
    }
    return *parent;
}

int main() {
    int T;
    scanf("%d", &T);

    while (T--) {
        int n, m;
        scanf("%d %d", &n, &m);

        int parent[MAX_TEAMS];
        for (int i = 1; i <= n; i++) {
            parent[i] = i;
        }

        for (int i = 0; i < m; i++) {
            int a, b;
            scanf("%d %d", &a, &b);

            int rootA = findRoot(parent, a);
            int rootB = findRoot(parent, b);

            if (rootA != rootB) {
                parent[rootA] = rootB;
            }
        }

        int count = 0;
        for (int i = 1; i <= n; i++) {
            if (parent[i] == i) {
                count++;
            }
        }

        printf("%d\n", count);
    }

}

